//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __ELASTOS_GRAFIX_COMM_H__
#define __ELASTOS_GRAFIX_COMM_H__

#ifndef GFX_EXPRESSION
#ifdef  _DEBUG
#define ERRORLEVEL      10
#define TRIPLEVEL       7
#define WARNING         5
#define NORMALLEVEL     6
#define INFORMATION     4
#define DEBUGLEVEL      0

#define GFX_EXPRESSION(n, expression)                               \
                do {                                                \
                if (n >= NORMALLEVEL) {                             \
                    switch(n) {                                     \
                    case ERRORLEVEL:                                \
                        kprintf("ERROR: ");                         \
                        break;                                      \
                                                                    \
                    case WARNING:                                   \
                        kprintf("WARNING: ");                       \
                        break;                                      \
                                                                    \
                    case INFORMATION:                               \
                        kprintf("INFORMATION: ");                   \
                        break;                                      \
                                                                    \
                    case DEBUGLEVEL:                                \
                        kprintf("DEBUG: ");                         \
                        break;                                      \
                    }                                               \
                    expression;                                     \
                    if (n >= 8) {                                   \
                        kprintf(":--%s--%d--\n", __FILE__, __LINE__);\
                    }                                               \
                }} while(0);
#else
#define GFX_EXPRESSION(n, expression)   do {} while(0)
#endif  // _DEBUG
#endif  // GFX_EXPRESSION

#define GFX_ERROR_MESSAGE(expre)    \
        GFX_EXPRESSION(ERRORLEVEL, kprintf("%s", #expre));
#define GFX_WARNING_MESSAGE(expre)  \
        GFX_EXPRESSION(WARNING, kprintf("%s", #expre));
#define GFX_DEBUG_MESSAGE(expre)    \
        GFX_EXPRESSION(DEBUGLEVEL, kprintf("%s", #expre));

#ifndef GFX_FAILED
#ifdef  _DEBUG
#define GFX_FAILED(ec)                                                  \
                (FAILED(ec)                                             \
                && (kprintf(">>>Grafix failed, ec = 0x%08x, %s, %d\n",  \
                    ec, __FILE__, __LINE__)))
#else
#define GFX_FAILED(ec)    FAILED(ec)
#endif // _DEBUG
#endif // GFX_FAILED

#ifndef GFX_EXPRESSION_TIP
#ifdef _DEBUG
#define GFX_EXPRESSION_TIP(expre)                                       \
        do {                                                            \
            ECode ec = (expre);                                         \
            if (FAILED(ec)) {                                           \
                kprintf(">>>Grafix failed, ec = 0x%08x, %s, %d\n",      \
                    ec, __FILE__, __LINE__);                            \
            }                                                           \
        } while(0);
#else
#define GFX_EXPRESSION_TIP(expre)   do {expre;} while(0);
#endif
#endif

#define TimerID_NoInput 0x10000000 // timer ID of input halt

//
// @internal
//
// Encodes format constants in the following way (bit 31 - 0):
//
// lkjj:hhgg | gfff:eeed | cccc:bbbb | baaa:aaaa
//
// a) pixelformat index<br>
// b) effective color (or index) bits per pixel of format<br>
// c) effective alpha bits per pixel of format<br>
// d) alpha channel present<br>
// e) bytes per "pixel in a row" (1/8 fragment, i.e. bits)<br>
// f) bytes per "pixel in a row" (decimal part, i.e. bytes)<br>
// g) smallest number of pixels aligned to byte boundary (minus one)<br>
// h) multiplier for planes minus one (1/4 fragment)<br>
// j) multiplier for planes minus one (decimal part)<br>
// k) color and/or alpha lookup table present<br>
// l) alpha channel is inverted
//

#define PIXELFORMAT(index, colorBits, alphaBits, hasAlpha,      \
                 rowBits, rowBytes, align, mulF, mulD,          \
                 hasLut, invAlpha )                             \
    ((((index) & 0x7F))                                         \
    | (((colorBits) & 0x1F) << 7)                               \
    | (((alphaBits) & 0x0F) << 12)                              \
    | (((hasAlpha ) ? 1 :0) << 16)                              \
    | (((rowBits) & 0x07) << 17)                                \
    | (((rowBytes) & 0x07) << 20)                               \
    | (((align) & 0x07) << 23)                                  \
    | (((mulF) & 0x03) << 26)                                   \
    | (((mulD) & 0x03) << 28)                                   \
    | (((hasLut) ? 1 :0) << 30)                                 \
    | (((invAlpha) ? 1 :0) << 31))

//
// These macros extract information about the pixel format.
//

#define PIXELFORMAT_INDEX(fmt)      (((fmt) & 0x0000007F))

#define COLOR_BITS_PER_PIXEL(fmt)   (((fmt) & 0x00000F80) >> 7)

#define ALPHA_BITS_PER_PIXEL(fmt)   (((fmt) & 0x0000F000) >> 12)

#define PIXELFORMAT_HAS_ALPHA(fmt)  (((fmt) & 0x00010000) !=  0)

#define BITS_PER_PIXEL(fmt)         (((fmt) & 0x007E0000) >> 17)

#define BYTES_PER_PIXEL(fmt)        (((fmt) & 0x00700000) >> 20)

#define BYTES_PER_LINE(fmt,width)   ((BITS_PER_PIXEL(fmt) * (width) + 7) >> 3)

#define PIXELFORMAT_ALIGNMENT(fmt)  (((fmt) & 0x03800000) >> 23)

#define PLANE_MULTIPLY(fmt,height)  ((((((fmt) & 0x3C000000) >> 26) + 4) \
                                            * (height)) >> 2)

#define PIXELFORMAT_IS_INDEXED(fmt) (((fmt) & 0x40000000) !=  0)

#define PLANAR_PIXELFORMAT(fmt)     (((fmt) & 0x3C000000) !=  0)

#define PIXELFORMAT_INV_ALPHA(fmt)  (((fmt) & 0x80000000) !=  0)



#define MAKE_GRAFIX_ENGINE_ECODE(severity, code) \
            (MAKE_ECODE(severity, FAMILY_GRAPHIC, 0x1) | (code & 0xFFFF))

#define MAKE_GRAFIX_CANVAS_ECODE(severity, code) \
            (MAKE_ECODE(severity, FAMILY_GRAPHIC, 0x2) | (code & 0xFFFF))

#define MAKE_GRAFIX_CONTROL_ECODE(severity, code) \
            (MAKE_ECODE(severity, FAMILY_GRAPHIC, 0x4) | (code & 0xFFFF))

#define E_UNKNOWN_FORMAT \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0001)
#define E_WINDOW_CLOSED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0002)
#define E_WINDOW_RECYCLE_OWN \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0003)
#define E_WINDOW_MORE_PRIORITY \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0004)
#define E_WINDOW_TIMER_TOO_MANY \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0005)
#define E_WINDOW_NOT_SUPPORT \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0006)
#define E_WINDOW_NOT_FOUND \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0007)
#define E_STACK_NOT_INITIALIZE \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0008)
#define E_STACK_BE_DESTROYED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0009)
#define E_LAYER_NOT_ENABLE \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000A)
#define E_SURFACE_DISABLED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000B)
#define E_SURFACE_IS_CHILD \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000C)
#define E_NOT_HAVE_EVENT_BUFFER \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000D)
#define E_SURFACE_NO_BUFFER \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000E)
#define E_SURFACE_LOCKED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x000F)
#define E_SURFACE_UNLOCK \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0010)
#define S_SURFACE_NO_BUFFER \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_SUCCESS, 0x0011)
#define E_WINDOW_NEED_PAINT \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0012)

#define E_FONT_UNINITIALIZED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0021)
#define E_FONT_NOT_FOUND \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0022)
#define E_CHAR_NOT_FOUND \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0023)

#define E_INIT_FONT_FAILED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0024)
#define E_CHAR_REND_FAILED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0025)
#define E_FONT_UNSUPPORTED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0026)
#define E_SIZE_UNSUPPORTED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0027)

#define E_BUFFER_NOT_MAPPED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0028)
#define E_BUFFER_MAPPED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x0029)
#define E_BUFFER_UNMAP_NOT_SAME_PROCESS \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x002A)
#define E_BUFFER_NOT_SUPPORT_MAP \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x002B)
#define E_BUFFER_MAP_FAILED \
            MAKE_GRAFIX_ENGINE_ECODE(SEVERITY_ERROR, 0x002C)

#endif  // __ELASTOS_GRAFIX_COMM_H__
